package com.my.blog.website.controller;

import com.my.blog.website.modal.Vo.CnbsUserVo;
import com.my.blog.website.modal.from.RegisterForm;
import com.my.blog.website.service.ICnbsUserService;
import com.my.blog.website.utils.TaleUtils;
import com.my.blog.website.utils.UUID;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;

/**
 * @Description 前台用户登录控制器
 * @ClassName LoginController
 * @Author gyh
 * @date 2020.08.13 15:36
 */
@Controller
@Slf4j
public class LoginController extends BaseController {

    @Resource
    ICnbsUserService userService;


    @GetMapping(value = "/toLogin")
    public String toLogin() {
        return "login";
    }

    @GetMapping(value = "/toRegister")
    public String toRegister() {
        return "register";
    }


    @PostMapping("/register")
    public String toRegister(RegisterForm registerForm, HttpServletRequest request, Model model) {
        log.info("注册表单信息：" + registerForm.toString());
        // 表单密码重复判断
        if (!registerForm.getPassword().equals(registerForm.getRepassword())) {
            model.addAttribute("registerMsg", "密码不一致");
            return "register";
        }
        //验证码比对
        String captcha = registerForm.getCode();
        String sessionCode = request.getSession().getAttribute("CAPTCHA").toString();
        if (sessionCode == null || StringUtils.isEmpty(sessionCode) || !captcha.equals(sessionCode)) {
            model.addAttribute("registerMsg", "车牌号输入错误");
            return "register";
        }
        // 用户名已存在
        CnbsUserVo hasUser = userService.getUserInfoByUserName(registerForm.getUsername());
        if (hasUser != null) {
            model.addAttribute("registerMsg", "用户名已存在");
            return "register";
        }
        //构建注册对象
        CnbsUserVo userVo = new CnbsUserVo();
        userVo.setUid(UUID.UU32());
        userVo.setRoleId(2);
        userVo.setAvatar("/user/img/avatar/" + new Random().nextInt(12) + ".jpg");
        userVo.setGmtCreate(new Timestamp(System.currentTimeMillis()));
        userVo.setLoginDate(new Timestamp(System.currentTimeMillis()));
        userVo.setUsername(registerForm.getUsername());
        // 密码加密
        String bCryptPassword = new BCryptPasswordEncoder().encode(registerForm.getPassword());
        userVo.setPassword(bCryptPassword);
        userService.save(userVo);
        log.info("新用户注册成功：" + userVo);
        return "redirect:/toLogin";
    }

    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int j = random.nextInt(12);
            System.out.println(j);
        }


    }
}
